// src/services/classroomBookingService.js
import api from './api'; // 确保你的 api 实例已经正确配置，例如 axios

const BASE_URL = '/api/classroom-booking';

/**
 * 教师提交教室借用申请
 * @param {Object} bookingData - 借用申请数据
 * @returns {Promise<Object>}
 */
export const applyBooking = async (bookingData) => {
    try {
        const response = await api.post(`${BASE_URL}/apply`, bookingData);
        return response.data;
    } catch (error) {
        console.error('提交借用申请失败:', error);
        throw error;
    }
};

/**
 * 教师查看自己的借用记录
 * @param {Object} params - 查询参数 { teacherId, classroomNumber, status, pageNum, pageSize }
 * @returns {Promise<Object>}
 */
export const getTeacherBookings = async (params) => {
    try {
        const response = await api.get(`${BASE_URL}/my`, { params });
        return response.data;
    } catch (error) {
        console.error('获取教师借用记录失败:', error);
        throw error;
    }
};

/**
 * 教师撤销借用申请 (仅限待审批)
 * @param {Long} id - 申请ID
 * @param {Long} teacherId - 教师ID
 * @returns {Promise<Object>}
 */
export const cancelBooking = async (id, teacherId) => {
    try {
        const response = await api.delete(`${BASE_URL}/cancel`, { params: { id, teacherId } });
        return response.data;
    } catch (error) {
        console.error('撤销借用申请失败:', error);
        throw error;
    }
};

/**
 * 教师修改借用申请 (仅限待审批)
 * @param {Object} bookingData - 修改后的借用申请数据
 * @returns {Promise<Object>}
 */
export const updateBooking = async (bookingData) => {
    try {
        const response = await api.put(`${BASE_URL}/update`, bookingData);
        return response.data;
    } catch (error) {
        console.error('修改借用申请失败:', error);
        throw error;
    }
};

/**
 * 获取所有教室列表 (假设有这个API，用于选择教室)
 * @returns {Promise<Object>}
 */
export const getAllClassrooms = async () => {
    try {
        // 假设后端有一个获取所有教室的API，例如 /api/classrooms
        const response = await api.get('/api/classrooms');
        return response.data;
    } catch (error) {
        console.error('获取教室列表失败:', error);
        throw error;
    }
};

/**
 * 获取教室可用时间 (假设有这个API)
 * @param {number} classroomId - 教室ID
 * @param {string} date - 查询日期 (YYYY-MM-DD)
 * @returns {Promise<Object>}
 */
export const getAvailableTimes = async (classroomId, date) => {
    try {
        // 假设后端有一个获取教室可用时间的API，例如 /api/classrooms/{classroomId}/available-times?date=...
        const response = await api.get(`${BASE_URL}/classrooms/${classroomId}/available-times`, { params: { date } });
        return response.data;
    } catch (error) {
        console.error('获取教室可用时间失败:', error);
        throw error;
    }
    
};
// /**
//  * 获取所有教室及其当天的借用状态
//  * @param {string} date - 查询日期 (YYYY-MM-DD)
//  * @returns {Promise<Object>}
//  */
// export const getAllClassroomsWithBookingStatus = async (date) => {
//     try {
//         const response = await api.get(`${BASE_URL}/classroom-status`, { params: { date } });
//         return response.data;
//     } catch (error) {
//         console.error('获取所有教室借用状态失败:', error);
//         throw error;
//     }
// };

/**
 * 获取所有教室及其指定时间段的借用状态
 * @param {string} queryStartTime - 查询开始时间 (YYYY-MM-DDTHH:mm:ss 格式)
 * @param {string} queryEndTime - 查询结束时间 (YYYY-MM-DDTHH:mm:ss 格式)
 * @returns {Promise<Object>}
 */
export const getAllClassroomsWithBookingStatus = async (queryStartTime, queryEndTime) => {
    try {
        const response = await api.get(`${BASE_URL}/classroom-status`, {
            params: {
                // 后端 @RequestParam 对应的参数名
                queryStartTime: queryStartTime,
                queryEndTime: queryEndTime
            }
        });
        return response.data;
    } catch (error) {
        console.error('获取所有教室借用状态失败:', error);
        throw error;
    }
};
